<template>
  <div>
    <div class="cate-wrap">
      <div class="cate-left">
        <div class="cates">
          <van-sidebar v-model="activeKey">
            <van-sidebar-item
              v-for="(cate, index) in cates"
              :key="cate.id"
              :title="cate.name"
              :class="[
                'cate',
                {
                  active: index === num,
                },
              ]"
              @click="switchTab"
            />
          </van-sidebar>
        </div>
      </div>
      <div class="cate-right">
        <div class="items">
          <div
            class="item"
            v-for="(item) in items"
            :key="item.id"
          >
            <img :src="item.pic" class="img" />
            <div class="item-info">
              <h5 class="name">{{ item.name }}</h5>
              <p class="desc">
                {{ item.characteristic ? item.characteristic : "暂无描述" }}
              </p>
              <div class="acion">
                <p class="price">￥{{ item.minPrice }}</p>
                <van-icon
                  size="25"
                  class-prefix="icon"
                  name="gouwuche"
                  @click="addCart(item.id)"
                />
              </div>
            </div>
          </div>
          <div v-if="!items.length" class="nothing">
              暂无商品哦~
          </div>
        </div>
      </div>
    </div>
    <common-tab />
  </div>
</template>

<script>
import commonTab from '../../components/commonTab'
import { fetchCates, fetchItems, addCart } from '../../api'
import { isLogin, getToken } from '../../utils'
import { Toast } from 'vant'
export default {
  name: 'cate',
  data () {
    return {
      activeKey: 0,
      cates: [],
      items: [],
      num: 0
    }
  },
  created () {
    this.fetchCates()
  },
  methods: {
    addCart (id) {
      if (!isLogin()) {
        Toast.fail({
          message: '请登录',
          duration: 1000,
          onClose: () => {
            // 跳转到登录页
            this.$router.push({
              path: '/login',
              query: {
                from: '/home'
              }
            })
          }
        })
        return false
      }
      addCart({
        goodsId: id,
        number: 1,
        token: getToken()
      }).then((res) => {
        if (res.data.code === 0) {
          Toast.success('加入成功，请前往购买')
          this.$store.commit('cart/set_items', res.data.data.items)
        }
      })
    },
    switchTab (index) {
      this.num = index
      this.fetchItems()
    },
    fetchCates () {
      fetchCates().then((res) => {
        if (res.data.code === 0) {
          this.cates = res.data.data
          this.fetchItems()
        }
      })
    },
    fetchItems () {
      this.items = []
      fetchItems({
        categoryId: this.cates[this.num].id
      }).then((res) => {
        if (res.data.code === 0) {
          this.items = res.data.data.result
          console.log(this.items)
        }
      })
    }
  },
  components: {
    commonTab
  }
}
</script>

<style lang="scss" scoped>
.nothing{
  text-align: center;
  color: #999;
  font-size: 18px;
  font-weight: 600;
  margin-top: 50px;
}
.cate-wrap {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  .cate-left {
    height: 100%;
    overflow-y: auto;
  }
  .cate-right {
    flex: 1;
    overflow-y: auto;
    .items {
      .item {
        border-top: 1px solid rgba(186, 225, 241, 0.856);
        border-bottom: 1px solid rgba(186, 225, 241, 0.856);
        height: 140px;
        display: flex;
        background-color: #fff;
        border-radius: 8px;
        overflow: hidden;
        align-items: center;
        .img {
          width: 120px;
          height: 120px;
          margin-top: 5px;
        }
        .item-info {
          flex: 1;
          height: 100%;
          padding-bottom: 30px;
          padding-left: 5px;
          .name {
            font-size: 14px;
            color: #333;
            margin-top: 40px;
            margin-left: 20px;
            line-height: 20px;
          }
          .desc {
            width: 100%;
            font-size: 12px;
            color: #666;
            margin-top: 20px;
            margin-left: 20px;
            line-height: 20px;
          }
          .acion {
            display: flex;
            padding-right: 40px;
            height: 50px;
            align-items: center;
            justify-content: space-between;
            font-size: 16px;
            margin-left: 20px;
            color: #ff734c;
          }
        }
      }
    }
  }
}
</style>
